-- ExpBoxView
-- 领取经验卡UI

local Armature = app:getClass("Armature")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.ExpGotScene

-- data = { expBoxRewardTypeList, expBoxLevel, msg.activeRewardInfos }
function ClassRef:init( data )
    self._lock = false -- 锁定状态

    self._ui.Button_quxiao:addClickEventListener(function()
        if not self._lock then
            self._lock = true
            self:closeView()
        end
    end)
    self._ui.Button_queding:addClickEventListener(function()
        if not self._lock then
            self._lock = true
            executeFunc(self._onOperate, "openExpBox", self._data[1])
            -- self:playSuccessAnim()
        end
    end)

    -- self._data[2]
    -- message expCardRewardInfo{
    --     int32 level = 1;    //奖励等级
    --     pb_loot.Loot loot = 2;  //奖励掉落
    -- }

    local rewardLoots = nil
    for k,v in pairs( self._data[2] ) do
        if tonumber( v.level )  == tonumber( self._data[1] ) then
            rewardLoots = v.loot
            break
        end
    end
    local rewardItem = UD:parseLootToBagItem( rewardLoots )
    if rewardItem then
        self._ui.tips_2:setString( string.format( L("lua_code_text_202"),  rewardItem.amount ) )
    else
        self._ui.tips_2:setString( string.format( L("lua_code_text_202"),  0 ) )
    end

    -- 隐藏动画
    self._ui.ani_expcard:setVisible(false)
end

-- 领取成功之后播放动画
function ClassRef:playSuccessAnim()
    self._ui.Panel:setVisible(false)

    self._ui.ani_expcard:setVisible(true)
    local timeline = self._ui.ani_expcard:getTimeline()
    timeline:gotoFrameAndPlay(0, false)

    local particle = self._ui.ani_expcard:findChild("Panel/flash_01")
    particle:resetSystem()

    local params = {
        armatureName = "kashan",
        path = Res.ExpCardEfcPath
    }
    local armature = Armature.create( params )
    self:addChild( armature )
    armature:setPosition( self._ui.Panel:convertToWorldSpace(self._ui.icon_1:getPosition()) )

    armature:play("shaizibengchu", 1, function(sender)
            self:_playEffect2()
        end)
end

-- 开始播放第二段特效
function ClassRef:_playEffect2()
    self:closeView()
end

return ClassRef
